<template>
  <div id="home">
    <el-container class="header-container">
      <el-header>
        <div class="header-logo">
          <img src="../assets/images/heima.png" alt="" />
          <div class="header-text">电商后台管理系统</div>
        </div>
        <el-button type="info" @click="goBack">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="flag ? '64px' : '200px'">
          <div class="toggle" @click="btnToggle">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            active-text-color="#409EFF"
            unique-opened
            :collapse="flag"
            :collapse-transition="false"
            router
            :default-active="erPath"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menusList"
              :key="item.id"
            >
              <template slot="title" class="el-title">
                <i :class="fontList[item.order]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="savePath('/' + subItem.path)"
              >
                <template slot="title" class="el-title">
                  <i class="el-icon-menu"></i>
                  {{ subItem.authName }}
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'home',
  data() {
    return {
      // 左侧导航菜单
      menusList: [],
      // 定义字体图标对象
      fontList: {
        1: 'iconfont icon-users',
        2: 'iconfont icon-tijikongjian',
        3: 'iconfont icon-shangpin',
        4: 'iconfont icon-danju',
        5: 'iconfont icon-baobiao',
      },
      flag: false,
      // 二级地址
      erPath: '',
    }
  },
  created() {
    this.getMenus()
    this.erPath = window.sessionStorage.getItem('address')
  },
  methods: {
    async getMenus() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menusList = res.data
    },
    // 退出按钮
    goBack() {
      this.$confirm('您确定退出登录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          window.sessionStorage.clear()
          this.$router.push('/login')
          this.$message({
            type: 'success',
            message: '退出成功!',
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出',
          })
        })
    },
    // 切换展开与收起
    btnToggle() {
      this.flag = !this.flag
      console.log(this.flag)
    },
    // 保存点击后二级菜单地址
    savePath(activePath) {
      window.sessionStorage.setItem('address', activePath)
      this.erPath = activePath
    },
  },
}
</script>

<style lang="less" scoped>
#home,
.header-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  .header-logo {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    img {
      width: 61px;
      height: 56px;
      margin-right: 15px;
    }
    .header-text {
      font-size: 22px;
      color: #fff;
    }
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
    /deep/ .el-submenu__title {
      font-size: 12px;
    }
    .el-menu-item {
      font-size: 12px;
    }
  }
}
.iconfont {
  margin-right: 6px;
  vertical-align: top;
}

.toggle {
  width: 100%;
  background-color: #4a5064;
  font-size: 12px;
  text-align: center;
  color: #fff;
  letter-spacing: 0.12em;
  line-height: 22px;
  cursor: pointer;
}
.el-main {
  background-color: #eaedf1;
}
</style>